﻿function btnBold_Click(ctrl)
{
    if (document.selection != undefined)
    {
        // IE version
        ctrl.focus();
        var sel = document.selection.createRange();
        var selectedText;
        
        selectedText = sel.text;
        sel.text = '[b]' + sel.text + '[/b]';
    }
    else if (ctrl.selectionStart != undefined)
    {
        //Firefox version
        var len = ctrl.value.length;
        var start = ctrl.selectionStart;
        var end = ctrl.selectionEnd;
        var sel = ctrl.value.substring(start, end);

        var replace = '[b]' + sel + '[/b]';

        // Here we are replacing the selected text with this new value
        ctrl.value = ctrl.value.substring(0, start) + replace + ctrl.value.substring(end, len);
    }
    
    window.event.returnValue = false;
}


function btnItalic_Click(ctrl)
{
    if (document.selection != undefined)
    {
        // IE version
        ctrl.focus();
        var sel = document.selection.createRange();
        var selectedText;
        selectedText = sel.text;
        sel.text = '[i]' + sel.text + '[/i]';
    }
    else if (ctrl.selectionStart != undefined)
    {
        //Firefox version
        var len = ctrl.value.length;
        var start = ctrl.selectionStart;
        var end = ctrl.selectionEnd;
        var sel = ctrl.value.substring(start, end);
        var replace = '[i]' + sel + '[/i]';
        // Here we are replacing the selected text with this new value
        ctrl.value = ctrl.value.substring(0, start) + replace + ctrl.value.substring(end, len);
    }
    window.event.returnValue = false;
}

function btnImage_Click(ctrl)
{
    if (document.selection != undefined)
    {
        // IE version
        ctrl.focus();
        var sel = document.selection.createRange();
        sel.text = '[img]' + sel.text + '[/img]';
    }
    else if (ctrl.selectionStart != undefined)
    {
        //Firefox version
        var len = ctrl.value.length;
        var start = ctrl.selectionStart;
        var end = ctrl.selectionEnd;
        var sel = ctrl.value.substring(start, end);

        var replace = '[img]webadres van de afbeelding[/img] ' + sel;

        // Here we are replacing the selected text with this new value
        ctrl.value = ctrl.value.substring(0, start) + replace + ctrl.value.substring(end, len);
    }

    window.event.returnValue = false;
}

function btnHyperlink_Click(ctrl)
{
    if (document.selection != undefined)
    {
        // IE version
        ctrl.focus();
        var sel = document.selection.createRange();
        
        if (sel.text == '')
        {
            sel.text = '[url=webadres van de link]tekst van de link[/url]';
        }
        else
        {
            sel.text = '[url=' + sel.text + ']tekst van de link[/url]';
        }
        
//        var saveText = ctrl.value;
//        ctrl.focus();

//        var range = document.selection.createRange();
//        var specialchar = String.fromCharCode(1);
//        range.text = specialchar;
//        var pos = ctrl.value.indexOf(specialchar);

//        ctrl.value = saveText;

//        range = ctrl.createTextRange();
//        range.move('character', pos);
//        range.select();
//        range.text = '[url=webadres van de link]tekst van de link[/url]';
//        ctrl.focus();
    }
    else if (ctrl.selectionStart != undefined)
    {
        //Firefox version
        var len = ctrl.value.length;
        var start = ctrl.selectionStart;
        var end = ctrl.selectionEnd;
        var sel = ctrl.value.substring(start, end);

        var replace = '[url=webadres van de link]tekst van de link[/url] ' + sel;

        // Here we are replacing the selected text with this new value
        ctrl.value = ctrl.value.substring(0, start) + replace + ctrl.value.substring(end, len);
    }

    window.event.returnValue = false;
}